<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\validate\Admin;
use think\exception\ValidateException;
use think\response\Json;
use app\admin\jwt\Jwts;

class Login extends Controller {

    /**
     * 用户登录
     * @return Json
     */
    public function index() {
        $_GPC = input();
        if ($this->request->isPost()) {
            if ($_GPC['username'] == '') {
                json_old('error', 1, '请填写帐号！');
            }
            if ($_GPC['pwd'] == '') {
                json_old('error', 1, '请填写密码！');
            }
            $data = pdo_get('admin', array('username' => $_GPC['username']));
            if (!$data) {
                json_old('error', 1, '帐号错误！');
            }
            if ($data['pwd'] != md5($_GPC['pwd'] . $data['salt'])) {
                json_old('error', 1, '密码错误！');
            }
            session('admininfo', base64_encode(json_encode($data)));
            json_old('success');
        }
        return $this->view->fetch();
    }
    public function out() {
         session('admininfo',null);
        $url = $this->request->domain(true) . '/admin/index/login';
        header('HTTP/1.1 303 See Other');
        header("Location: $url");
        exit;
    }

}
